package com.infore.airsupervise.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.FrameLayout;

import com.infore.airsupervise.R;
import com.infore.airsupervise.app.Constants;
import com.infore.airsupervise.app.StatusConst;
import com.infore.airsupervise.bean.LawingSiteE;
import com.infore.airsupervise.biz.SiteLawingBiz;
import com.infore.airsupervise.biz.impl.SiteLawingBizImpl;
import com.infore.airsupervise.http.OnServerResponseListener;
import com.infore.airsupervise.ui.fragment.SiteAskDetailFragment;
import com.infore.airsupervise.ui.fragment.SiteInspectDetailFragment;
import com.infore.airsupervise.ui.fragment.SiteRecordDetailFragment;
import com.infore.airsupervise.ui.widget.EmptyView;
import com.infore.airsupervise.utils.ToastUtil;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by xiaotao on 2017/5/31.
 */

public class SitePartDetailActivity extends BaseTitleBarActivity {
    @BindView(R.id.layout_content)
    FrameLayout layoutContent;
    private LawingSiteE.RecordFormat format; //id 和 实体类类型
    private SiteLawingBiz siteLawingBiz;
    private EmptyView emptyView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_site_part_detail);
        ButterKnife.bind(this);
        Intent it = getIntent();
        if (null != it) {
            format = (LawingSiteE.RecordFormat) it.getSerializableExtra(Constants.INTENT_OBJ_KEY);
        }
        title.setText(StatusConst.modelTypeTransfer(format.getType()));
        rightMenu.setVisibility(View.INVISIBLE);
        siteLawingBiz = new SiteLawingBizImpl();
        getDatas();
    }

    private void getDatas() {
        siteLawingBiz.sendSiteLedgerDeatilRequest(format.getId(), format.getType(), new OnServerResponseListener() {
            @Override
            public void onSucceed(Object result, String desc) {
                try {
                    layoutContent.removeAllViews();
                    getSupportFragmentManager().beginTransaction()
                            .replace(R.id.layout_content, getTypeFragment(format.getType(), result)).commit();
                } catch (Exception ex) {
                    ex.printStackTrace();
                    ToastUtil.showShort(ctx, "界面数据解析失败");
                }

            }

            @Override
            public void onFailed(int errorCode, String title, String desc) {
                initEmptyView(errorCode, title, desc);
            }
        });
    }

    /**
     * @param type   类型标识
     * @param result
     * @return
     */
    private Fragment getTypeFragment(int type, Object result) {
        if (type == StatusConst.SITE_LEDGER_ASK) {
            return new SiteAskDetailFragment(result);
        } else if (type == StatusConst.SITE_LEDGER_CHECK) {
            return new SiteRecordDetailFragment(result);
        } else if (type == StatusConst.SITE_LEDGER_INSPECT) {
            return new SiteInspectDetailFragment(result);
        }
        return null;
    }

    /**
     * 设置填充的空试图
     * @param errorCode
     * @param title
     * @param desc
     */
    private void initEmptyView(int errorCode, String title, String desc) {
        if (null == emptyView) {
            emptyView = new EmptyView(ctx);
            emptyView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    getDatas();
                }
            });
        }
        int imgRes = R.drawable.status_connect_failed;
        switch (errorCode) {
            case StatusConst.CODE_CONNECT_FAIL:
                imgRes = R.drawable.status_connect_failed;
                break;

            case StatusConst.CODE_CONNECT_TIMEOUT:
                imgRes = R.drawable.status_timeout;
                break;

            case StatusConst.CODE_SERVER_EXCEPTION:
                imgRes = R.drawable.status_server_fix;
                break;

            case StatusConst.CODE_OPERATION_FAIL:
                imgRes = R.drawable.status_connect_failed;
                break;
        }
        emptyView.setShowInfo(imgRes, title);
        layoutContent.removeAllViews();
        layoutContent.addView(emptyView);
    }
}